<template>
	<view class="">
		<!-- 自定义导航栏 -->
		<u-navbar back-text="返回" title="书库"></u-navbar>

		<!-- 筛选 -->
		<u-dropdown>
			<u-dropdown-item v-model="currentType" :title="typeTitle" :options="typeList" @change="onFilterChange"></u-dropdown-item>
			<u-dropdown-item v-model="currentOverType" :title="overTypeTitle" :options="overTypeList" @change="onFilterChange"></u-dropdown-item>
		</u-dropdown>

		<!-- 数据列表 -->
		<mescroll-uni :top="top" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
			<view class="u-p-30" style="width:100vw">
				<detail-card class="u-m-b-30" v-for="item in list" :key="item.book_id" :bookId="item.book_id" :name="item.name" :image="item.image"
				 :writer="item.writer" :is_over="item.is_over" :intro="item.intro" :type="item.type"></detail-card>
			</view>
		</mescroll-uni>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		getList
	} from '@/api/list.js'
	import {
		getBookType
	} from '@/api/common.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				currentType: 1,
				currentOverType: 0,
				typeList: [],
				overTypeList: [{
						label: '全部',
						value: 0,
					},
					{
						label: '连载',
						value: 1,
					},
					{
						label: '完结',
						value: 2,
					},
				],
				list: [],
				upOption: {
					textNoMore: '没有更多了',
					noMoreSize: 4
				},
				page: 1,
				pageSize: 20
			}
		},
		computed: {
			typeTitle() {
				return this.typeList.find(item => item.value === this.currentType)?.label
			},
			overTypeTitle() {
				return this.overTypeList.find(item => item.value === this.currentOverType)?.label
			},
			...mapState({
				iStatusBarHeight: state => state.global.iStatusBarHeight,
			}),
			top() {
				return this.iStatusBarHeight + 196 + 30
			}
		},
		methods: {
			async init() {
				if (this.typeList.length === 0) {
					const {
						data
					} = await getBookType()
					this.typeList = data.bookType
					this.typeList.forEach(item =>{
						item.label = item.name
						item.value = item.id
					})
					this.currentType = this.typeList[0].id
				}
				getList({
						type: this.currentType,
						isOver: this.currentOverType,
						page: this.page,
						pageSize: this.pageSize
					})
					.then((res) => {
						this.list = [...this.list, ...res.data.list]
						this.page++
						this.mescroll.endSuccess(res.data.list.length)
					})
					.catch((e) => {
						console.log(e)
					})
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll
			},
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			// 上拉
			upCallback() {
				this.init()
			},
			onFilterChange() {
				this.list = []
				this.mescroll.resetUpScroll()
			}
		}
	}
</script>

<style>
	.slot-wrap {
		display: flex;
		align-items: center;
		/* 如果您想让slot内容占满整个导航栏的宽度 */
		/* flex: 1; */
		/* 如果您想让slot内容与导航栏左右有空隙 */
		/* padding: 0 30rpx; */
	}
</style>
